package com.antler.smt.admin.controller;

import com.antler.smt.admin.annotation.OperateCut;
import com.antler.smt.admin.mapper.MessagePushEntityMapper;
import com.antler.smt.entity.MessagePushEntity;
import com.antler.smt.entity.PurchaserChargeEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.request.base.BasePageReq;
import com.antler.smt.request.portal.MessagePushReq;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.admin.MessagePushService;
import com.antler.smt.utils.LogUtil;
import com.antler.smt.validate.DataValidator;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.LifecycleState;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.List;

/**
 * 超级管理员推送消息给采购商或者服务商
 *
 * @author zhangyi
 * @date 2018/5/17 15:12
 */
@RestController
@RequestMapping("/messagePush")
@Api(description = "后台消息推送")
public class MessagePushController {
    /**
     * 日志
     */
    private static Logger log = LogUtil.get();

    @Resource
    private MessagePushService messagePushService;

    /**
     *  写入推送消息
     *
     * @return
     */
    @PostMapping("/writePushMessage")
    @ApiOperation(value = "写入推送消息（type:1采购商2服务商3管理员）", httpMethod = "POST")
    @OperateCut(content = "写入推送消息")
    public Result<ResultCodeEnum> writePushMessage(@RequestBody  MessagePushReq req){

        DataValidator.isNull(req,"推送消息不能为空！");
        DataValidator.isBlank(String.valueOf(req.getType()),"用户类型不能为空！");
        DataValidator.isBlank(req.getPushContent(),"推送消息内容不能为空！");
        DataValidator.isNull(req.getMessageTitle(),"推送标题不能为空！");
        Result result = null;
            result = messagePushService.writeMessage(req);
        return result;
    }
    /**
     *  删除消息
     *
     * @return
     */
    @PostMapping("/delMessage")
    @ApiOperation(value = "删除消息", httpMethod = "POST")
    @OperateCut(content = "删除消息")
    public Result<ResultCodeEnum> delMessage(@RequestParam(value = "messageId") Long messageId){

        DataValidator.isBlank(String.valueOf(messageId),"消息id不能为空！");

        Result result = null;
            result = messagePushService.del(messageId);
        return result;
    }
    /**
     *  分类获取消息列表（1:采购商 2：服务商 3：管理员的消息）
     *
     * @return
     */
    @GetMapping("/getMessagePushList")
    @ApiOperation(value = "分类获取消息列表（1:采购商 2：服务商 3：管理员的消息）用,隔开", httpMethod = "GET")
    @OperateCut(content = "分类获取消息")
    public Result<PageInfo<MessagePushEntity>> getMessagePushList(BasePageReq req,
                                                                  @RequestParam(value = "status") String status){

        DataValidator.isNull(req,"推送信息分页实体不能为空！");

        Result result = null;
            String[] split = status.split(",");
            List<Integer> list = new ArrayList<>();
            for(String s : split){
                list.add(Integer.parseInt(s));
            }
            result = messagePushService.getMessagePushList(req,list);
        return result;
    }
}
